<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/4/17
 * Time: 11:53
 * @copyright: ©2024 永州微聚 商网络科技有限公司
 * @link: https://www.weijuYunke.com
 */

namespace app\admin\controller;


use app\admin\model\ImgLog;
use app\admin\model\OperateLog;
use app\Request;
use OSS\OssClient;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Filesystem;
class  Log{
    public function index(Request $request){
        $param = $request->param();
        $limit  =  10;
        $start = 0;
        $where = [];
        if (isset($param['user_type']) && !empty($param['user_type'])){
            $where['user_type'] = $param['user_type'];
        }
        if (isset($param['limit']) &&  !empty($param['limit'])){
            $limit = $param['limit']  ;
        }
        if (isset($param['page']) &&  !empty($param['page'])){
            $start = ($param['page'] -1)  *  $limit;
        }
        $operateLogModel = new OperateLog();
        $list = $operateLogModel->alias('o')
            ->order('create_time','desc')
            ->leftJoin('staff_account s', 's.id = o.operate_id')
            ->field('o.*,s.name')
          ->limit($start,$limit)->select()->toArray();
        $count =  $operateLogModel->count();
        return adminSuccess('操作日记',['list' => $list,'count' => $count]);


    }
    public function imgLog(Request $request){
        $param = $request->param();
        $imgLogModel = new ImgLog();
        $limit  =  10;
        $start = 0;
        if (isset($param['limit']) &&  !empty($param['limit'])){
            $limit = $param['limit']  ;
        }
        if (isset($param['page']) &&  !empty($param['page'])){
            $start = ($param['page'] -1)  *  $limit;
        }
        $imgLog =  $imgLogModel->limit($start,$limit)->select()->toArray();
        $count = $imgLogModel->count();
        return adminSuccess('操作日记',['list' => $imgLog,'count' => $count]);
    }

    public  function uploadImg(Request $request)
    {
        $configModel = new \app\home\model\Config();
        $value = $configModel::getConfigValue('upload_way');
        $files = $request->file("file");
        if (!$files) {
            return adminError('请上传文件');
        }
        $ext = $files->extension();
        $size = filesize($files);
        if ($value == 1 || $ext == 'pem') {
            try {
                $path = 'uploads';
                // 将图片保存至本地
                $saveName = Filesystem::disk('public')->putFile($path, $files);
                $saveName = $request->domain() . '/storage/' . $saveName;
                $url = $saveName;
                /*    return success('上传成功', ['url' => $saveName]);*/
                //将文件上传的路径返回
            } catch (\think\exception\ValidateException $e) {
                return adminError($e->getMessage());
            }
        } else if ($value == 2) {
            $fileType = $configModel::getConfigValue('qiniu_file_type');
            if ($fileType) {
                $fileTypeArray = explode(',', $fileType);
                if (!in_array($ext, $fileTypeArray)) {
                    return adminError('此类型文件不允许上传');
                }
            }
            $qiniu_access_key = $configModel::getConfigValue('qiniu_access_key');
            $qiuniu_secret_bucket = $configModel::getConfigValue('qiniu_secret_key');
            $qiniu_bucket = $configModel::getConfigValue('qiniu_bucket');
            // 构建Auth对象
            $auth = new Auth($qiniu_access_key, $qiuniu_secret_bucket);
            // 要上传的空间
            $bucket = $qiniu_bucket;
            // 生成上传Token
            $token = $auth->uploadToken($bucket);
            // 上传到七牛后的文件名
            $key = date('Y-m-d') . '/' . substr(md5($files->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $files->extension();
            // 初始化UploadManager类
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $files->getRealPath());
            if ($err !== null) {
                adminError('上传失败');
            } else {
                $url = $configModel::getConfigValue('qiniu_domin') . '/' . $ret['key'];
            }
        } else {
            $fileType = $configModel::getConfigValue('ali_file_type');
            $fileTypeArray = explode(',', $fileType);
            if (!in_array($ext, $fileTypeArray)) {
                return adminError('此类型文件不允许上传');
            }
            $accessKeyId = $configModel::getConfigValue('ali_access_key');
            $accessKeySecret = $configModel::getConfigValue('ali_secret_key');
            $endpoint = $configModel::getConfigValue('ali_domain');
            $bucket = $configModel::getConfigValue('ali_bucket');
            //实例化对象 将配置传入
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            //这里是有sha1加密 生成文件名 之后连接上后缀
            $key = date('Y-m-d') . '/' . substr(md5($files->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $files->extension();
            //上传至阿里云的目录 为年+月/日的格式
            //执行阿里云上传 bucket名称,上传的目录,文件
            $result = $ossClient->uploadFile($bucket, $key, $files->getRealPath());
            $url =  $result['info']['url'];

            //将结果输出
        }
        $imgLogModel = new ImgLog();
        $imgLogModel->insert(['img' =>$url ]);
        return adminSuccess('上传成功',['url' =>$url ]);
    }

    public  function uploadDel(Request $request){
        $param = $request->param();
        $imgLogModel = new ImgLog();
        $imgLogModel->where(['id' =>$param['id']])->delete();
        return adminSuccess('成功' );
    }

}